package com.banshuibao.bsb_android.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CommitAnswerModel;
import com.banshuibao.bsb_android.model.UnAnswerModel;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.LogUtil;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.QuickDoubleClickable;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.XToast;

public class CommitOneOnActivity extends BaseActivity implements View.OnClickListener {

    private TextView commit;
    private TextView content;
    private TextView date;
    private MyEditText myEditText;
    private String uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commit_one_on);
        initView();
        getData();
    }

    private void getData() {
        UnAnswerModel.DataBean item = (UnAnswerModel.DataBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            uuid = item.getUuid();
            String content1 = item.getContent();
            long createDate = item.getCreateDate();
            setData(content1, createDate);
        }
    }

    private void setData(String content1, long createDate) {
        if (!TextUtils.isEmpty(content1)) {
            content.setText(content1);
        }
        String time = null;
        if (createDate != 0) {
            time = TimeUtil.format(createDate);
        } else {
            time = "";
        }
        date.setText(time);
    }

    private void initView() {
        setTitleText("问题详情");
        commit = (TextView) findViewById(R.id.menu);
        commit.setVisibility(View.VISIBLE);
        commit.setText("提交");
        commit.setOnClickListener(this);
        content = (TextView) findViewById(R.id.content);
        date = (TextView) findViewById(R.id.date);
        myEditText = (MyEditText) findViewById(R.id.quick_edit);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                if (QuickDoubleClickable.isDoubleClick())
                    return;
                String content = myEditText.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    showProgressDialog();
                    setPost(content);
                } else {
                    XToast.showToast(this, "请输入答案");
                }
                break;
        }
    }

    private void setPost(String content) {
        CommitAnswerModel model = new CommitAnswerModel();
        model.setContent(content);
        if (!TextUtils.isEmpty(uuid)) {
            model.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.ANSWER_ASK, model, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                cancelProgressDialog();
                XToast.showToast(CommitOneOnActivity.this, "回答成功");
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }
}
